package com.nsoft.server.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.nsoft.server.master.beans.PartyMaster;
import com.nsoft.server.service.dao.PartyDao;
import com.nsoft.server.service.main.PartyService;
import com.nsoft.thickclient.beanUtils.BeanUtilities;
import com.nsoft.thickclient.trackMove.intr.party.Party;
import com.nsoft.thickclient.trackMove.intr.party.ThinParty;

public class PartyServiceImpl implements PartyService {

	public PartyDao partyDao;

	public PartyDao getPartyDao() {
		return partyDao;
	}

	public void setPartyDao(PartyDao partyDao) {
		this.partyDao = partyDao;
	}

	@Transactional
	public Party saveParty(Party party) {
		PartyMaster partyMaster = null;
		// PartyMasterPK masterPK = null;
		if (party.getId() == null) {
			partyMaster = new PartyMaster();
			// masterPK = new PartyMasterPK();
		} else {
			partyMaster = partyDao.findById(new Integer(party.getId()
					.intValue()), false);
			// masterPK = partyMaster.getPk();
		}
		partyMaster.setPartyCharName(party.getName());
		partyMaster.setPartyCharExim(party.getExim());
		partyMaster.setPartyCharPan(party.getPan());
		partyMaster.setPartyCharTan(party.getTan());
		partyMaster.setPartyCharTin(party.getTin());

		// partyMaster.setPk(masterPK);
		partyMaster.setPartyCharHomeaddress(party.getHomeAddr());
		partyMaster.setPartyCharRphone(party.getHomePhone());
		partyMaster.setPartyCharOphone(party.getOfficePhone());
		partyMaster.setPartyDateCreated(party.getCrteationDate());
		// partyMaster.setPartyNumParentid()
		partyMaster.setPartyCharNotes(party.getRemarks());
		partyMaster.setPartyCharAddress(party.getHomeAddr());

		partyMaster = partyDao.makePersistent(partyMaster);
		party.setId(new Long(partyMaster.getPartyNumId()));

		return party;
	}

	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public PartyMaster getPartydetails(Long id) {
		return partyDao.findById(new Integer(id.intValue()), false);
	}

	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<ThinParty> getAllThinParties(ThinParty thinParty) {
		return partyDao.getAllThinParties(thinParty);
	}

	public List<Party> searchParties(Party party) {
		List<PartyMaster> list = partyDao.searchParties(party);
		List<Party> partyList = new ArrayList<Party>();
		String[] sourceNames = new String[] { "partyNumId", "partyCharName",
				"partyCharTan", "partyCharTin", "partyCharExim",
				"partyCharPan", "partyCharAddress", "partyCharHomeaddress",
				"partyCharOphone", "partyCharRphone", "partyCharContactperson",
				"partyCharContactmobile", "partyCharNotes", "partyDateCreated" };

		String[] destinationNames = new String[] { "id", "name", "tan", "tin",
				"exim", "pan", "officeAddr", "homeAddr", "officePhone",
				"homePhone", "contactPerson", "contactPhone", "remarks",
				"crteationDate" };

		for (PartyMaster partyMaster : list) {
			Party party2 = new Party();
			party2.setId(new Long(partyMaster.getPartyNumId()));
			BeanUtilities.copyProerties(partyMaster, party2, sourceNames,
					destinationNames);
			partyList.add(party2);
		}
		return partyList;
	}

}
